<template>
	<view class="content">
		<view class="mainTitle">车牌号</view>
        <creat-carNo @listenPlateChange='getCarNo' ref='creatCartNo' :defaultPlate="form.licensePlate"></creat-carNo>
		<view class="box formbox">
			<view class="form-item" @click="$refs.creatCartNo.panelHide()">
				<view class="form-left">
					<view class="name">颜色</view>
					<view class="value">
						<input type="text" v-model="form.cartColor" placeholder="请输入颜色" />
					</view>
				</view>
			</view>
			<view class="form-item" @click="$refs.creatCartNo.panelHide()">
			  <picker @change="bindPickerChange" range-key='dictDataName' :value="currCartType" :range="cartTypes">
				<view class="form-left">
					<view class="name">车辆类型</view>
						<view class="value">
							<view style="color:#02B1AA">{{form.cartType?form.cartType:'请选择车辆类型'}}</view>
						</view>
				</view>
				<uni-icons class="form-right" type='right' size="16"></uni-icons>
			  </picker>
			</view>
		</view>
		<view class="box uploadImagebox">
			<view class="upload-tip">
				<view class="tip">上传图片</view>
				<view class="again-upload" v-if='showUpload'  @click="chooseImage">
					<text style="color:#02B1AA">重新上传</text>
					<uni-icons class="form-right" type='right' size="16"></uni-icons>
				</view>
			</view>
			<view class="uploadImage">
				<view class="upload" v-if='!showUpload' @click="chooseImage">
					<image :src="$getImageSrc('ic_sc.png')" mode="heightFix"></image>
				</view>
				<image :src="form.cartImg" class="showImage" mode="heightFix" v-else></image>
			</view>
		</view>
		<view class="box setting">
			<view class="form-item">
				<view class="form-left">
					<view class="name">设为默认车辆</view>
				</view>
				<view class="form-right">
					<switch checked  @change="switchChange" color="#02B1AA" style="transform:scale(0.6)" v-if="form.showable" />
					<switch  @change="switchChange" color="#02B1AA" style="transform:scale(0.6)" v-else />
				</view>
			</view>
		</view>
		<view style="height: 200rpx;"></view>
		<footer-button btnTip='保存车辆信息' @clickFooter='saveCarFn'></footer-button>
	</view>
</template>

<script>
	import {addCart, cartTypes, editCart} from '@/untils/api'
	export default {
		data() {
			return {
				showUpload: false,
				form:{
				  cartColor:'',
				  cartType:'',
				  licensePlate:'',
				  cartImg:'',
				  showable:false
				},
				currCartType: 0,
				cartTypes:[]
			}
		},
		// onLoad(option) {
		// 	if(option.id){
		// 		uni.setNavigationBarTitle({
		// 			title:'修改车辆'
		// 		})
		// 		option.showable = JSON.parse(option.showable)
		// 		this.form = option
		// 		this.showUpload = true
		// 	}
  //         this.getCartTypes()
		// },
		mounted() {
			let id = this.$mp.query.id;
			if(id){
				// #ifdef MP-WEIXIN
				// #endif
				uni.setNavigationBarTitle({
					title:'修改车辆'
				})
				
				
				this.form = this.$mp.query
				this.showUpload = true
			}
			this.getCartTypes()
		},
		methods: {
		  getCartTypes(){
			  cartTypes({cartType:1}).then((res) => {
				  this.cartTypes = res.data
			  })
		  },
		  bindPickerChange(e){
			  this.form.cartType = this.cartTypes[e.detail.value].dictDataName
		  },
          getCarNo(No){
			let carNo = No.join('')
			this.form.licensePlate = carNo
			if(carNo.length>=7){
				this.$refs.creatCartNo.panelHide()
			}
		  },
		  isValidCarNumber(No){
		   let pattern7 =
			  /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽赣贵粤青藏川宁琼使领][A-Z][A-Z0-9]{4}[A-Z0-9挂学警港澳]$/;
			let pattern8 =
			  /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[A-HJ-K]$)|([A-HJ-K][A-HJ-NP-Z0-9][0-9]{4}$))/;
			const len = vno.length;
			if (len == 7) {
			  return pattern7.test(vno);
			} else if (len == 8) {
			  return pattern8.test(vno);
			}
			return false;
		  },
		  switchChange(e){
			  this.form.showable = e.detail.value
		  },
		  chooseImage(){
			  this.$refs.creatCartNo.panelHide()
			  this.$uploadFile().then((url) => {
                this.showUpload = true;
				this.form.cartImg = url
			  })
		  },
		  updatePrePage(){
			  let pages = getCurrentPages();
			  let prePage = pages[pages.length - 1]
			  prePage.$vm.getMyCarts();
		  },
		  saveCarFn(){
			  let title = ''
			  if(!this.form.licensePlate){
			  	 title = '请输入车牌号'
			  }
			  if(!this.form.cartColor){
				  title = '请输入颜色'
			  }
			  if(!this.form.cartImg){
				  title = '请上传车辆图片'
			  }
			  if(!this.form.cartType){
				  title = '请选择车辆类型'
			  }
			  if(title){
				uni.showToast({
					icon:'none',
					title: title,
					duration: 3000
				}); 
				return false;
			  }
			  let api = addCart
			  if(this.form.id){
				  api = editCart
			  }
			  api(this.form).then((res) => {
				  if(res.code - 200 === 0){
					  uni.showToast({
					  	icon:'success',
					  	title: title,
					  	duration: 2000,
					  	complete:() => {
							uni.navigateBack({
								delta:1,
								success:() => {
									this.updatePrePage()
								}
							})
					  	}
					  });
				  }else{
					  uni.showToast({
						icon:'none',
						title: res.msg,
						duration: 3000
					}); 
				  }
			  })
		  }
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		background: #f8f8f8;
		height: 100%;
		overflow: auto;
		.formbox{
			margin-top: 30rpx;
		}
		.form-item{
			height: 88rpx;
	        line-height: 88rpx;
			border-bottom: 1px solid #f8f8f8;
			overflow: hidden;
			font-size: 30rpx;
			width: 90%;
			margin: 0 auto;
			.form-left{
				float: left;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				.name{
					width: 180rpx;
				}
			}
			.form-right{
				float: right;
			}
		}
		.uploadImagebox{
			margin-top: 30rpx;
			box-sizing: border-box;
			padding: 20rpx;
			font-size: 30rpx;
			.upload-tip{
				display: flex;
				justify-content: space-between;
				align-items: center;
				line-height: 88rpx;
				height:88rpx
			}
			.uploadImage{
				overflow: hidden;
				width: 100%;
				.upload{
					width: 100%;
					height: 240rpx;
					background: #eee;
					display: flex;
					justify-content: center;
					align-items:center;
					image{
						height: 240rpx;
					}
				}
				.showImage{
					height: 240rpx;
				}
			}
		}
		.setting{
			margin-top: 30rpx;
		}
	}
</style>
